//
//  TTSpecialItemViewController.m
//  TaoTuo
//
//  Created by LG on 2020/10/19.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTSpecialItemViewController.h"
#import "TTHomeGridViewCell.h"
#import "TTGoodsDetailViewController.h"

static NSString *cellID = @"cell";

@interface TTSpecialItemViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *gridLayout;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, copy) NSString *activityId;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) UIImageView *bgImageView;
@property (nonatomic, strong) UIButton *backBtn;

@end

@implementation TTSpecialItemViewController

- (instancetype)initWithActivityId:(NSString *)activityId
{
    self = [super init];
    if (self) {
        self.activityId = activityId;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.fd_prefersNavigationBarHidden = YES;
    
    self.page = 1;
    
    _backBtn = [UIButton createButtonWithImage:[UIImage imageNamed:@"detail_nav_back"] target:self action:@selector(backAction)];
    _backBtn.frame = CGRectMake(0, kStatusBarHeight, 44, 44);
    [self.view insertSubview:_backBtn aboveSubview:self.collectionView];
    
    @weakify(self)
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self)
        self.page = 1;
        [self getActivityDetailListDatas];
    }];
    
    self.collectionView.mj_footer = [MJRefreshBackFooter footerWithRefreshingBlock:^{
        @strongify(self)
        self.page++;
        [self getActivityDetailListDatas];
    }];
    
    [self getActivityDetailListDatas];
    
}

- (void)getActivityDetailListDatas
{
    NSDictionary *dic = @{
        @"param.pageIndex" : @(self.page),
        @"param.pageSize" : @20,
        @"param.activityId" : self.activityId
    };
    @weakify(self)
    [[LLNetworkManagerTool sharedManager] requestMethod:GET urlString:activityDetailList_api parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        @strongify(self)
        if (self.page == 1) {
            [self.dataArray removeAllObjects];
        }
        
        self.title = responseObject[@"Name"];
        NSString *colorStr = responseObject[@"BackgroundColor"];
        self.collectionView.backgroundColor = [UIColor colorWithHexString:colorStr];
        NSString *imageUrlStr = responseObject[@"BackgroundImg"];
        [[SDWebImageManager sharedManager] loadImageWithURL:[NSURL URLWithString:imageUrlStr] options:SDWebImageRetryFailed progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
            if (image) {
                self.bgImageView.image = image;
                CGFloat imageViewH = image.size.height * self.view.width / image.size.width;
                self.bgImageView.frame = CGRectMake(0, 0, self.view.width, imageViewH);
                self.gridLayout.sectionInset = UIEdgeInsetsMake(imageViewH, 10, 0, 10);
                [self.collectionView reloadData];
            }
        }];
        
        NSArray *array = responseObject[@"StyleLibrary"][@"Data"];
        for (NSDictionary *dic in array) {
            TTHomeListModel *model = [TTHomeListModel modelWithJSON:dic];
            [self.dataArray addObject:model];
        }
        
        [self.collectionView.mj_header endRefreshing];
        if (array.count < 20) {
            [self.collectionView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self.collectionView.mj_footer endRefreshing];
        }
        
        [self.collectionView reloadData];
        
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        
        NSLog(@">> %@", error);
        
    } showLoadingInView:nil];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TTHomeListModel *model = self.dataArray[indexPath.row];
    TTHomeGridViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    cell.model = model;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    TTHomeListModel *model = self.dataArray[indexPath.row];
    TTGoodsDetailViewController *detailVC = [[TTGoodsDetailViewController alloc] initWithStyleLibraryId:model.StyleLibraryId model:model];
//    detailVC.styleLibraryId = model.StyleLibraryId;
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((self.view.width - 30) * 0.5, 175 * kScale + 60);
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    return [UIImage imageNamed:@"tao_nodata"];
}

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        self.gridLayout = [[UICollectionViewFlowLayout alloc] init];
        self.gridLayout.sectionInset = UIEdgeInsetsMake(0, 10, 10, 10);
        self.gridLayout.minimumLineSpacing = 10;
        self.gridLayout.minimumInteritemSpacing = 10;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.gridLayout];
        _collectionView.backgroundColor = UIColor.whiteColor;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.emptyDataSetSource = self;
        _collectionView.emptyDataSetDelegate = self;
        [_collectionView registerClass:[TTHomeGridViewCell class] forCellWithReuseIdentifier:cellID];
        [self.view addSubview:_collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_topLayoutGuideTop).offset(-kStatusBarHeight);
            make.left.right.equalTo(self.view);
            make.bottom.equalTo(self.mas_bottomLayoutGuideBottom);
        }];
    }
    return _collectionView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (UIImageView *)bgImageView
{
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] init];
        _bgImageView.contentMode = UIViewContentModeScaleAspectFill;
        [self.collectionView addSubview:_bgImageView];
    }
    return _bgImageView;
}

@end
